package com.xiaochong.tir.manager.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Summary;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.SummaryService;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.ConceptCategory;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.ConceptCategoryService;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

/**   
 * @ClassName:  ConceptCategoryController   
 * @Description:TODO(概念管理)   
 * @author:  mxh
 * @date:   2018年5月7日 下午5:14:14   
 */
@Controller
@RequestMapping("/manage/conceptCategory")
@Api(value = "概念管理")
public class ConceptCategoryController {
	
	@Autowired
	private ConceptCategoryService conceptCategoryService;
	@Autowired
	private RedisServer redisServer;
	@Autowired
	private SummaryService summaryService;

	@RequestMapping(value = "/conceptCategoryList", method = RequestMethod.GET)
	public String syChannelsList() {
		return "/conceptCategory/conceptCategoryList";
	}
	
	@RequestMapping(value = "/addConceptCategory", method = RequestMethod.GET)
	public String addSyChannels() {
		return "/conceptCategory/conceptCategoryAdd";
	}

	/**   
	 * @Title: listConceptCategory   
	 * @Description: TODO(列表)   
	 * @param: @param find
	 * @param: @param page
	 * @param: @param limit
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "查询概念列表",notes = "查询概念列表")
	@RequestMapping(value = "/listConceptCategory")
	@ResponseBody
	public Object listConceptCategory(@RequestParam(value = "find", required = false) String find,
			@RequestParam("page") int page,@RequestParam("limit") int limit) {
		try {
			PageInfo<ConceptCategory> pageInfo = conceptCategoryService.getConceptCategoryList(find,page, limit);
			return new ReturnResult<ConceptCategory>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/listConceptCategory", "[/manage/conceptCategory/listConceptCategory error {}]",
					e);
		}
		return null;
	}

	
	/**   
	 * @Title: saveConceptCategory   
	 * @Description: TODO(新增)   
	 * @param: @param conceptCategory
	 * @param: @param session
	 * @param: @param model
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "新增概念",notes = "新增概念")
	@RequestMapping("/saveConceptCategory")
	public Object saveConceptCategory(ConceptCategory conceptCategory) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"新增概念");
		try {
			ConceptCategory category = conceptCategoryService.selectByEntity(conceptCategory);
			if(null != category){
				return "/conceptCategory/conceptCategoryList";
			}
			//填入概念简介
			Summary summary = new Summary();
			summary.setType("4");
			summary.setRelationId(conceptCategory.getId());
			Summary old = summaryService.selectByEntity(summary);
			if(null == old){
				summary.setContent(conceptCategory.getContent());
				summary.setCreateTime(new Date());
				summary.setUpdateTime(new Date());
				summaryService.insertSelective(summary);
			}
			conceptCategory.setEnableFlag(conceptCategory.getStatus().equals(1)?true:false);
			conceptCategory.setDeleteFlag(false);
			conceptCategory.setCreateTime(new Date());
			conceptCategory.setUpdateTime(new Date());
			conceptCategoryService.insertSelective(conceptCategory);
			return "/conceptCategory/conceptCategoryList";
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveConceptCategory", e.getMessage(),e);
		}
		return null;
	}
	
	/**   
	 * @Title: updateConceptCategory   
	 * @Description: TODO(修改)   
	 * @param: @param conceptCategory
	 * @param: @param session
	 * @param: @param model
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "编辑概念",notes = "编辑概念")
	@RequestMapping("/updateConceptCategory")
	public Object updateConceptCategory(ConceptCategory conceptCategory) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"编辑概念");
		try {
			//编辑概念简介
			Summary summary = new Summary();
			summary.setType("4");
			summary.setRelationId(conceptCategory.getId());
			Summary old = summaryService.selectByEntity(summary);
			summary.setContent(conceptCategory.getContent());
			summary.setUpdateTime(new Date());
			summary.setCreateTime(new Date());
			if(null == old){
				summaryService.insertSelective(summary);
			}else{
				summaryService.updateBySummary(summary);
			}
			ConceptCategory gory = conceptCategoryService.selectByPrimaryKey(conceptCategory.getId());
			conceptCategory.setEnableFlag(gory.getEnableFlag());
			conceptCategory.setDeleteFlag(false);
			conceptCategory.setUpdateTime(new Date());
			conceptCategoryService.updateByPrimaryKey(conceptCategory,gory);
			return "/conceptCategory/conceptCategoryList";
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/updateConceptCategory", e.getMessage(),e);
		}
		return null;
	}
	
	/**   
	 * @Title: updateConceptCategoryStatus   
	 * @Description: TODO(改状态)   
	 * @param: @param id
	 * @param: @param enableFlag
	 * @param: @return      
	 * @return: ResultEntity<JSONObject>      
	 * @throws   
	 */
	@ApiOperation(value = "修改概念状态",notes = "修改概念状态")
	@GetMapping("/updateConceptCategoryStatus")
    @ResponseBody
    public ResultEntity<JSONObject> updateConceptCategoryStatus(@RequestParam("id") Integer id, @RequestParam("enableFlag") String enableFlag){
		redisServer.set(RedisKeysUtils.LOGINNAME,"概念启用/停用");
		ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
        	ConceptCategory conceptCategory = new ConceptCategory();
        	conceptCategory.setId(id);
        	conceptCategory.setEnableFlag("true".equals(enableFlag)?false:true);
        	conceptCategory.setUpdateTime(new Date());
        	conceptCategoryService.updateByPrimaryKey(conceptCategory,conceptCategoryService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName() + "/updateConceptCategoryStatus", e.getMessage(),e);
        }
        return result;
    }

}
